package cn.hxth.cloud.provider.updater;

import cn.hxth.cloud.api.common.util.DateProvider;
import cn.hxth.cloud.api.domain.Product;
import cn.hxth.cloud.api.mapper.ProductMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

public abstract class AbstractProductUpdater implements ProductUpdater {
    private Logger logger = LoggerFactory.getLogger(AbstractProductUpdater.class);
    protected List<Product> products;
    protected ProductMapper productMapper;
    protected DateProvider dateProvider;

    public AbstractProductUpdater(List<Product> products, ProductMapper productMapper, DateProvider dateProvider) {
        this.products = products;
        this.productMapper = productMapper;
        this.dateProvider = dateProvider;
    }

    @Override
    public void updateProduct() {
        try {

                this.updateLockedInventory();
            this.updateInventory();
             this.execute();
        } catch (Exception e) {
            logger.error("error", e);
        }


    }

    protected abstract void updateLockedInventory();
    protected abstract void updateInventory();
    private void execute() {
        for (Product product : products) {
            this.productMapper.updateByPrimaryKey(product);
        }
    }
}
